//
//  VCShowTicketBodyView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/22.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class ShowTicketBodyView: UIView {
    
    // MARK: 订单数据
    public var tickets:[TicketInfo]? {
        didSet {
            updateLayout()
        }
    }
    
    // 修改下单文本
    public var submitText:String?
    
    // 回调
    public var complete: ((_ item:Int) -> Void)?
    
    // 滑动Scrollview
    private let scrollView = UIScrollView()
    // 没有更多数据
    private let nomoredata = NoMoreDataView()
    
    override func layoutSubviews() {
        guard let infos = tickets else {
            return
        }
        // 设置scrollview
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        // 记录条目高度
        var orderHeight:CGFloat = 0
        // 开始摆放布局
        for i in 0..<infos.count {
            let ticket = infos[i]
            // 背景颜色
            let backround = UIView()
            // 票价符号
            let symbol = UILabel()
            symbol.textColor = COLOR_RED
            symbol.text = "¥"
            symbol.font = UIFont.systemFont(ofSize: 14)
            symbol.frame = CGRect(x: MARGIN, y: MARGIN20 , width: frame.width, height: MARGIN)
            backround.addSubview(symbol)
            // 票价
            let ticketPriceLabel = UILabel()
            ticketPriceLabel.textColor = COLOR_RED
            ticketPriceLabel.text = ticket.ticket_price
            ticketPriceLabel.font = UIFont.systemFont(ofSize: 18)
            ticketPriceLabel.frame = CGRect(x: MARGIN25, y: MARGIN , width: frame.width, height: MARGIN)
            backround.addSubview(ticketPriceLabel)
            // 出发时间
            let startTime = UILabel()
            startTime.text = ticket.depart_time
            startTime.textColor = UIColor.black
            startTime.font = UIFont.boldSystemFont(ofSize: 23)
            startTime.frame = CGRect(x: MARGIN, y: ticketPriceLabel.frame.maxY + MARGIN , width: MARGIN140, height: MARGIN20)
            backround.addSubview(startTime)
            // 到达时间
            let arrivalTerminal = UILabel()
            arrivalTerminal.text = ticket.arrival_time
            arrivalTerminal.textAlignment = .right
            arrivalTerminal.textColor = UIColor.black
            arrivalTerminal.font = UIFont.boldSystemFont(ofSize: 23)
            arrivalTerminal.frame = CGRect(x: frame.width * 0.7 - MARGIN140 - MARGIN, y: ticketPriceLabel.frame.maxY + MARGIN , width: MARGIN140, height: MARGIN20)
            backround.addSubview(arrivalTerminal)
            // 箭头图片
            let arrowPic = UIImageView(image: UIImage(named: "checi_arrow"))
            arrowPic.frame.origin = CGPoint(x: center.x * 0.7 - arrowPic.frame.width * 0.5, y: ticketPriceLabel.frame.maxY + MARGIN30 )
            backround.addSubview(arrowPic)
            // 飞行时长
            let miletime = UILabel()
            miletime.text = ticket.miletime
            miletime.textColor = UIColor.gray
            let mileTextWidth = miletime.text!.boundingRect(w: MARGIN50, h: MARGIN, fontSize: 13).width * 0.5
            let offset = arrowPic.bounds.size.width * 0.5 - mileTextWidth
            miletime.font = UIFont.systemFont(ofSize: 13)
            miletime.frame = CGRect(x: center.x * 0.7 - arrowPic.frame.width * 0.5 + offset, y: arrowPic.frame.maxY - 18, width: MARGIN50, height: MARGIN)
            backround.addSubview(miletime)
            // 出发航站楼
            let departTime = UILabel()
            departTime.textColor = COLOR_NICE
            departTime.text = ticket.city! + "机场" + ticket.depart_terminal!
            departTime.font = UIFont.systemFont(ofSize: 12)
            departTime.frame = CGRect(x: MARGIN, y: arrivalTerminal.frame.maxY + MARGIN , width: MARGIN140, height: MARGIN)
            backround.addSubview(departTime)
            // 到达航站楼
            let arrivalTime = UILabel()
            arrivalTime.textAlignment = .right
            arrivalTime.textColor = COLOR_NICE
            arrivalTime.text = ticket.endcity! + "机场" + ticket.arrival_terminal!
            arrivalTime.font = UIFont.systemFont(ofSize: 12)
            arrivalTime.frame = CGRect(x: frame.width * 0.7 - MARGIN140 - MARGIN, y: arrivalTerminal.frame.maxY + MARGIN , width: MARGIN140, height: MARGIN)
            backround.addSubview(arrivalTime)
            // 舱位名称
            let cabinnameLabel = UILabel()
            cabinnameLabel.textColor = COLOR_NICE
            cabinnameLabel.text = ticket.cabinname
            cabinnameLabel.font = UIFont.systemFont(ofSize: 12)
            cabinnameLabel.frame = CGRect(x: frame.width - MARGIN50, y: arrivalTerminal.frame.maxY + MARGIN , width: MARGIN100, height: MARGIN)
            backround.addSubview(cabinnameLabel)
            // 订布局
            let orderView = OrderView()
            orderView.tag = i
            orderView.submitText = submitText ?? "订"
            orderView.layer.cornerRadius = 4
            orderView.count = ticket.ticket_count!
            orderView.layer.masksToBounds = true
            orderView.backgroundColor = COLOR_BACKGROUND
            orderView.frame = CGRect(x: frame.width - 54, y: cabinnameLabel.frame.maxY - 70, width: MARGIN45, height: MARGIN50)
            backround.addSubview(orderView)
            // 下划线
            let line2 = UIView()
            if i != infos.count - 1 {
                line2.backgroundColor = COLOR_LINE
            }
            line2.frame = CGRect(x: MARGIN, y: arrivalTime.frame.maxY + MARGIN20, width: frame.width - MARGIN * 2, height: 1)
            backround.addSubview(line2)
            // 背景颜色
            backround.backgroundColor = COLOR_LIGHT_WHITE2
            backround.frame = CGRect(x: 0, y: orderHeight, width: frame.width, height: line2.frame.maxY)
            scrollView.addSubview(backround)
            // 每条高度
            orderHeight += line2.frame.maxY
            // 订单按钮 添加监听事件
            orderView.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
        }
        // 没有更多数据
        nomoredata.frame = CGRect(x: 0, y: orderHeight, width: frame.width, height: MARGIN55)
        scrollView.addSubview(nomoredata)
        // 滑动
        scrollView.showsVerticalScrollIndicator = false
        scrollView.contentSize = CGSize(width: 0, height: nomoredata.frame.maxY)
        addSubview(scrollView)
    }
    
    @objc func clickItem(btn:UIButton) -> Void {
        if let callback = complete {
            callback(btn.tag)
        }
    }
}
